package com.hcryan.djm.service.impl;

import com.github.pagehelper.PageInfo;
import com.hcryan.djm.common.init.LocationInitial;
import com.hcryan.djm.constant.LocationType;
import com.hcryan.djm.pojo.model.Location;
import com.hcryan.djm.service.LocationService;
import com.hcryan.djm.utils.page.PageInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ryanwdj
 * @since 2025/5/5
 */
@Service
@Slf4j
public class LocationServiceImpl implements LocationService {

    @Override
    public PageInfo<Location> getLocationPage(String name, int pageNum, int pageSize) {
        Map<LocationType, List<Location>> locationMap = LocationInitial.getLocationMap();
        List<Location> pLocation = locationMap.get(LocationType.PROVINCE);
        List<Location> cLocation = locationMap.get(LocationType.CITY);
        List<Location> aLocation = locationMap.get(LocationType.AREA);

        List<Location> result = new ArrayList<>();
        List<Location> filterAreaLocation = aLocation.stream().filter(location -> StringUtils.isNotEmpty(location.getAreaName()) && location.getAreaName().contains(name)).collect(Collectors.toList());
        List<Location> filterCityLocation = cLocation.stream().filter(location -> StringUtils.isNotEmpty(location.getCityName()) && location.getCityName().contains(name)).collect(Collectors.toList());
        List<Location> filterProvinceLocation = pLocation.stream().filter(location -> StringUtils.isNotEmpty(location.getProvinceName()) && location.getProvinceName().contains(name)).collect(Collectors.toList());

        result.addAll(filterAreaLocation);
        result.addAll(filterCityLocation);
        result.addAll(filterProvinceLocation);

        PageInfo<Location> pageInfo = PageInfoUtil.getPageInfo(result, pageNum, pageSize);
        return pageInfo;

    }
}
